package com.lj.api.v1.c;


import com.alibaba.fastjson.JSONObject;
import com.lj.api.common.Result;
import com.lj.mcore.entity.*;
import com.lj.mcore.service.IGoodsSpecificationService;
import com.lj.mcore.utils.CommUtils;
import com.lj.mcore.utils.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/c")
public class GoodsTypeController {

    @Autowired
    IGoodsSpecificationService goodsSpecificationService;

    @RequestMapping("/gspecList")
    public String gspecList(String currentPage,String name,String per){
        int page = CommUtils.null2Int(currentPage)==0?1:CommUtils.null2Int(currentPage);
        int pageSize = 200;
        if (!CommUtils.null2String(per).equals("")) {
            pageSize = CommUtils.null2Int(per);
        }

        Map query = new HashMap();
        query.put("name",CommUtils.null2String(name));
        PageBean pagebean = goodsSpecificationService.getList(pageSize,page,query);
        List<GoodsSpecification> list = pagebean.getList();

        List list1 = new ArrayList();
        for(GoodsSpecification specs:list){
            Map map = new HashMap();
            map.put("id",specs.getId());
            map.put("name",specs.getName());
            map.put("sequence",specs.getSequence());
            map.put("type",specs.getType());

            List<GoodsSpecProperty> list2 = specs.getProperties();
            String val = "";
            for (GoodsSpecProperty gs:list2){
                val+= gs.getValue()+",";
            }
            if (val.length()>0){
                val=val.substring(0,val.length()-1);
            }
            map.put("vals",val);
            list1.add(map);
        }
        Map info = new HashMap();
        info.put("objs", JSONObject.toJSON(list1));
        info.put("totalPage", pagebean.getTotalPage());
        info.put("pageSize", pagebean.getPageSize());
        info.put("rows", pagebean.getAllRow());
        info.put("currentPage", pagebean.getCurrentPage());
        return Result.ok(info);
    }

}
